import {
  Body,
  Controller,
  Delete,
  Get,
  Headers,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { DictDataService } from './dict_data.service';
import { create, update, getList, deleteDictCode } from './dict_data.dto';
import { Result } from 'src/result/Result';

@Controller('/dict_data')
export class DictDataController {
  constructor(private readonly dictDataService: DictDataService) {}

  // 新增字典数据
  @Post()
  async create(
    @Body()
    {
      dictLabel,
      dictType,
      dictValue,
      dictSort,
      status,
      remark,
      listClass,
      cssClass,
      isDefault,
    }: create,
    @Headers('authorization') token: string,
  ) {
    const { count } = await this.dictDataService.create(
      {
        dictSort,
        dictType,
        dictValue,
        dictLabel,
        remark,
        listClass,
        status,
        cssClass,
        isDefault,
      },
      token,
    );
    return Result.toAjax(count);
  }

  // 修改字典数据
  @Put()
  async update(
    @Body()
    {
      dictLabel,
      dictType,
      dictValue,
      dictSort,
      remark,
      dictCode,
      status,
      listClass,
      cssClass,
    }: update,
    @Headers('authorization') token: string,
  ) {
    const { count } = await this.dictDataService.update(
      {
        dictSort,
        dictType,
        dictValue,
        dictLabel,
        remark,
        dictCode,
        status,
        listClass,
        cssClass,
      },
      token,
    );
    return Result.toAjax(count);
  }

  // 查询字典数据列表
  @Get('/list')
  async getList(@Query() { pageNum, dictType, pageSize }: getList) {
    const data = await this.dictDataService.getList({
      pageNum,
      pageSize,
      dictType,
    });
    return Result.ok(data);
  }

  // 根据字典类型查询字典数据信息
  @Get('/type/:dictType')
  async getDictType(@Param('dictType') dictType: string) {
    const rows = await this.dictDataService.getDictType(dictType);
    return Result.ok(rows);
  }

  // 查询字典数据详细
  @Get(':dictCode')
  async getDictCode(@Param('dictCode') dictCode: string) {
    console.log(dictCode);
    const data = await this.dictDataService.getDictCode(dictCode);
    return Result.ok(data);
  }

  // 删除字典数据
  @Delete(':dictCodes')
  async deleteDictCode(@Param() { dictCodes }: deleteDictCode) {
    const { count } = await this.dictDataService.deleteDictCode({ dictCodes });
    return Result.toAjax(count);
  }
}
